// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace std;
using namespace boost::posix_time;

__int64 GetPerformanceFrequency()
{
	static LARGE_INTEGER frequency = {0};
	if (frequency.QuadPart == 0)
	{
		::QueryPerformanceFrequency(&frequency);
	}
	return frequency.QuadPart;
}

__int64 GetPerformanceTickCount(__int64 frequency)
{
	LARGE_INTEGER time = {0};
	::QueryPerformanceCounter(&time);
	return time.QuadPart/frequency;
}


class CElapsed
{
public:
	CElapsed()
	{
		LARGE_INTEGER frequency = {0};
		if (frequency.QuadPart == 0)
		{
			::QueryPerformanceFrequency(&frequency);
		}
		m_frequency = frequency.QuadPart;

		LARGE_INTEGER startTime = {0};
 		m_start = microsec_clock::universal_time();		

		::QueryPerformanceCounter(&startTime);
		m_beginTime = startTime.QuadPart;

	}

	~CElapsed()
	{
 		Sleep(50);
		LARGE_INTEGER stopTime = {0};
	 	m_end = microsec_clock::universal_time();

		char elapsedTime[MAX_PATH] = "";
// 		__int64 elapsed1 = m_endTime-m_beginTime;
		double elapsed2 = ((double)(m_endTime-m_beginTime) / (double)m_frequency) * 1000;
		sprintf_s(elapsedTime,"[%f]",elapsed2);
// 		sprintf_s(elapsedTime,"[%I64d]",elapsed2);

		string strTime = to_simple_string(microsec_clock::local_time());
		cout << strTime.c_str() << endl;
		cout << "Elapsed" << elapsedTime << "ms" << endl;
 		cout << "Elapsed" << m_end - m_start << endl;
 		double ns = (double)(m_end.time_of_day().total_milliseconds() - m_start.time_of_day().total_milliseconds());
 		cout << "Elapsed" << ns << "ms" << endl;

	}

private:
	__int64 m_frequency;
	__int64 m_beginTime;
	__int64 m_endTime;
	ptime m_start;
	ptime m_end;
};

void TestElapsed()
{
	CElapsed elapsed1;
}


int _tmain(int argc, _TCHAR* argv[])
{
	TestElapsed ();

	Sleep(10000);
	return 0;
}

